﻿using EmgrMvc.Models;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EmgrMvc.BLL
{
    public class ScheduleTool
    {
        public static IScheduler scheduler = null;
        public async static void Init()
        {
            var schedulerFactory = new StdSchedulerFactory();
            scheduler = await schedulerFactory.GetScheduler();
            await scheduler.Start();
            Console.WriteLine($"任务调度器已启动");
        }

        public async static void TestTask()
        {
            //创建作业和触发器
            var jobDetail = JobBuilder.Create<SayHelloJob>()
                .WithIdentity("id1", "group1")
                .SetJobData(new JobDataMap() {
                                new KeyValuePair<string, object>("UserName", "Tom")
                            }).Build();
            var trigger = TriggerBuilder.Create()
                                        //.WithCronSchedule("0 3 6 * * ?")
                                        //.WithSimpleSchedule(m => m.WithRepeatCount(3).WithIntervalInMinutes(1))
                                        .StartAt(DateTime.Now + TimeSpan.FromSeconds(30))
                                        .Build();

            //添加调度
            await scheduler.ScheduleJob(jobDetail, trigger);
        }
    }
}
